package com.boardgame.gwt.service.operate.action.platcard;

import com.boardgame.common.entity.ResponseInfo;
import com.boardgame.gwt.data.GwtGameBoard;
import com.boardgame.gwt.data.GwtPlayerInfo;
import com.boardgame.gwt.data.card.BaseCard;
import com.boardgame.gwt.finalinfo.SyntaxMatchFinal;
import com.boardgame.gwt.service.operate.SyntaxRequest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 打对子得4元
 */
public class PlayPairCardAction extends PlayCardAction {
    @Override
    protected int getGainCoin() {
        return 4;
    }

    @Override
    protected String getCardColor() {
        return null;
    }

    @Override
    public ResponseInfo action(GwtGameBoard gwtGameBoard, SyntaxRequest syntaxRequest, Matcher matcher) {
        super.action(gwtGameBoard, syntaxRequest, matcher);
        //打牌的编号
        int code_a = Integer.parseInt(matcher.group(1));
        int code_b = Integer.parseInt(matcher.group(2));
        GwtPlayerInfo gwtPlayerInfo = this.gwtGameBoard.getCurrentPlayer();
        //判断是否有这张牌
        BaseCard card_a = gwtPlayerInfo.getListCardHand().stream().filter(baseCard -> {return baseCard.getCode() == code_a;}).findFirst().orElse(null);
        BaseCard card_b = gwtPlayerInfo.getListCardHand().stream().filter(baseCard -> {return baseCard.getCode() == code_b;}).findFirst().orElse(null);
        if (card_a==null || card_b == null){
            return ResponseInfo.Failed("确定手牌中包含次两张牌!");
        }
        else {
            //手牌删除这样
            gwtPlayerInfo.getListCardHand().remove(card_a);
            gwtPlayerInfo.getListCardHand().remove(card_b);
            //进入弃牌
            gwtPlayerInfo.getListCardDiscard().add(card_a);
            gwtPlayerInfo.getListCardDiscard().add(card_b);
            //检测颜色
            if (!(card_a.getColor().equals(card_b.getColor()))){
                return ResponseInfo.Failed("打出卡牌颜色不同!");
            }
            //获得钱
            gwtPlayerInfo.setCoins(this.getGainCoin());
        }

        return ResponseInfo.Success(null);
    }

    @Override
    public Pattern getPattern() {
        return Pattern.compile(SyntaxMatchFinal.PLAY_PAIR_Card_ACTION);
    }

    public static PlayPairCardAction create(){
        return new PlayPairCardAction();
    }
}
